home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / CNews / Source / batch / batcher.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-05  |  2.4 KB  |  117 lines

  1. /*
  2.  * batcher - send a bunch of news articles as an unbatch script
  3.  *
  4.  * Usage: batcher listfile
  5.  *
  6.  *    where listfile is a file containing a list, one per line, of
  7.  *    names of files containing articles.  Only the first
  8.  *    field of each line is looked at, so there can be more if needed
  9.  *    for other things.  Non-absolute pathnames are understood to lie
  10.  *    under the current directory; chdiring to the right place is the
  11.  *    parent's problem.
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <signal.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include "fgetmfs.h"
  20.  
  21. #ifndef READSIZE
  22. #define READSIZE 8192    /* allows for even 4.2 worst case file systems */
  23. #endif
  24. char buffer[READSIZE];
  25.  
  26. char *progname;
  27.  
  28. int debug = 0;            /* Debugging? */
  29.  
  30. main(argc, argv)
  31. int argc;
  32. char *argv[];
  33. {
  34.     int c;
  35.     int errflg = 0;
  36.     extern int optind;
  37.     extern char *optarg;
  38.     register FILE *list;
  39.     char *article;
  40.     int ret;
  41.  
  42.     progname = argv[0];
  43.     while ((c = getopt(argc, argv, "x")) != EOF)
  44.         switch (c) {
  45.         case 'x':    /* Debugging. */
  46.             debug++;
  47.             break;
  48.         case '?':
  49.         default:
  50.             errflg++;
  51.             break;
  52.         }
  53.     if (errflg || optind != argc-1) {
  54.         (void) fprintf(stderr,
  55.             "Usage: batcher listfile\n");
  56.         exit(2);
  57.     }
  58.  
  59.     list = fopen(argv[optind], "r");
  60.     if (list == NULL)
  61.         error("unable to open `%s'", argv[optind]);
  62.  
  63.     while ((article = fgetms(list)) != NULL) {
  64.         process(article);
  65.         free(article);
  66.     }
  67.     if (!feof(list))
  68.         error("fgetms failure (read error or out of memory) in `%s'",
  69.                                 argv[optind]);
  70.  
  71.     exit(0);
  72. }
  73.  
  74. /*
  75.  - process - process an article
  76.  */
  77. process(article)
  78. register char *article;
  79. {
  80.     register int artfile;
  81.     register int count;
  82.     struct stat sbuf;
  83.     register char *endp;
  84.  
  85.     endp = strchr(article, '\t');
  86.     if (endp == NULL)
  87.         endp = strchr(article, ' ');
  88.     if (endp == NULL)
  89.         endp = strchr(article, '\n');
  90.     if (endp != NULL)
  91.         *endp = '\0';
  92.  
  93.     artfile = open(article, 0);
  94.     if (artfile < 0) {
  95.         /*
  96.          * Can't read the article.  This isn't necessarily a
  97.          * disaster, since things like cancellations will do
  98.          * this.  Mumble and carry on.
  99.          */
  100.         if (debug)
  101.             warning("can't find `%s'", article);
  102.         return;
  103.     }
  104.  
  105.     if (fstat(artfile, &sbuf) < 0)
  106.         error("internal disaster, can't fstat", "");
  107.  
  108.     printf("#! rnews %ld\n", sbuf.st_size);
  109.     while ((count = read(artfile, buffer, sizeof buffer)) > 0)
  110.         if (fwrite(buffer, sizeof(char), count, stdout) != count)
  111.             error("write failure in `%s'", article);
  112.     if (count < 0)
  113.         error("read failure in `%s'", article);
  114.  
  115.     (void) close(artfile);
  116. }
  117.